{% extends "zato/index.html" %}
{% load extras %}

{% block html_title %}Microsoft 365 connections{% endblock %}

{% block "extra_css" %}
    <link rel="stylesheet" type="text/css" href="/static/css/jquery.alerts.css">
    {% if items %}
    <style type="text/css">
        .ui-dialog-titlebar {
            margin-bottom:-4px !important;
        }
    </style>
    {% endif %}
{% endblock %}

{% block "extra_js" %}

    {% comment %} jQuery {% endcomment %}
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.tablesorter.min.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.cookie.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.alerts.min.js"></script>

    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/common.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/cloud/microsoft_365.js"></script>

    <script nonce="{{ CSP_NONCE }}">
    $.fn.zato.data_table.get_columns = function() {
        return [
            '_numbering',
            '_selection',

            {% comment %} 1 {% endcomment %}
            'name',
            '_is_active',
            'tenant_id',
            'client_id',

            {% comment %} 2 {% endcomment %}
            '_edit',
            '_delete',

            {% comment %} 3 {% endcomment %}
            '_ping',
            'secret_value',
            'scopes',

            {% comment %} 4 {% endcomment %}
            'id',
            'is_active',
        ]
    }
    </script>

{% endblock %}

{% block "content" %}
<h2 class="zato">Microsoft 365 connections</h2>

{% if not zato_clusters %}
    {% include "zato/no-clusters.html" %}
{% else %}

    <div id="user-message-div" style='display:none'><pre id="user-message" class="user-message"></pre></div>

{% with has_generic="1" generic_type="cloud-microsoft-365" %}
    {% include "zato/search-form.html" with page_prompt="Show connections"%}
{% endwith %}

    {% if cluster_id %}
        <div class='page_prompt' style="clear:left">
            <a href="javascript:$.fn.zato.cloud.microsoft_365.create()">Create a new connection</a>
        </div>

        <div id="markup">
            <table id="data-table">
                <thead>
                    <tr class='ignore'>
                        <th>&nbsp;</th>
                        <th>&nbsp;</th>

                        {% comment %} 1 {% endcomment %}
                        <th><a href="#">Name</a></th>
                        <th><a href="#">Active</a></th>
                        <th><a href="#">Tenant ID</a></th>
                        <th><a href="#">Client ID</a></th>

                        {% comment %} 2 {% endcomment %}
                        <th>&nbsp;</th>
                        <th>&nbsp;</th>

                        {% comment %} 3 {% endcomment %}
                        <th>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        {% comment %} 4 {% endcomment %}
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                </thead>

                <tbody>
                {% if items %}
                {% for item in items %}
                    <tr class="{% cycle 'odd' 'even' %}" id='tr_{{ item.id }}'>
                        <td class='numbering'>&nbsp;</td>
                        <td class="impexp"><input type="checkbox" /></td>

                        {% comment %} 1 {% endcomment %}
                        <td>{{ item.name }}</td>
                        <td>{{ item.is_active|yesno:'Yes,No' }}</td>
                        <td>{{ item.tenant_id }}</td>
                        <td>{{ item.client_id }}</td>

                        {% comment %} 2 {% endcomment %}
                        <td><a href="javascript:$.fn.zato.cloud.microsoft_365.edit('{{ item.id }}')">Edit</a></td>
                        <td><a href="javascript:$.fn.zato.cloud.microsoft_365.delete_('{{ item.id }}')">Delete</a></td>

                        {% comment %} 3 {% endcomment %}
                        <td><a href="javascript:$.fn.zato.data_table.ping('{{ item.id }}')">Ping</a></td>
                        <td class='ignore'>{{ item.secret_value }}</td>
                        <td class='ignore'>{{ item.scopes }}</td>

                        {% comment %} 4 {% endcomment %}
                        <td class='ignore item_id_{{ item.id }}'>{{ item.id }}</td>
                        <td class='ignore'>{{ item.is_active }}</td>

                    </tr>
                {% endfor %}
                {% else %}
                    <tr class='ignore'>
                        <td colspan='15'>No results</td>
                    </tr>
                {% endif %}

                </tbody>
            </table>
        </div>

{% include "zato/paginate/action-panel.html" %}

        <div id="create-div" class='data-popup ignore'>
            <div class="bd">
                <form novalidate action="{% url "cloud-microsoft-365-create" %}" method="post" id="create-form">
                    <table class="form-data">
                        <tr>
                            <td style="vertical-align:middle;width:13%">
                                <label for="id_name">
                                Name
                                </label>
                            </td>
                            <td>{{ create_form.name }}</td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <label>
                                Active
                                {{ create_form.is_active }}
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="id_tenant_id">
                                Tenant ID
                                </label>
                            </td>
                            <td>
                                {{ create_form.tenant_id }}
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="id_client_id">
                                Client ID
                                </label>
                            </td>
                            <td>
                                {{ create_form.client_id }}
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="id_secret_value">
                                Secret value
                                </label>
                            </td>
                            <td>
                                {{ create_form.secret_value }}
                            </td>
                        </tr>
                        <tr>
                            <td style="vertical-align:middle">
                            OAuth2
                            </td>
                            <td>
                            <a href="javascript:$.fn.zato.toggle_visibility('.oauth2-options-block')">Toggle options</a>
                            </td>
                        </tr>
                        <tr class="oauth2-options-block hidden">
                            <td>
                                <label for="id_scopes">
                                Scopes
                                </label>
                            </td>
                            <td>
                                {{ create_form.scopes }}
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align:right">
                                <input type="submit" value="OK" />
                                <button type='button' onclick='javascript:$.fn.zato.data_table.close(this)'>Cancel</button>
                            </td>
                        </tr>
                    </table>
                    <input type="hidden" id="cluster_id" name="cluster_id" value="{{ cluster_id }}" />
                </form>
            </div>
        </div>

        <div id="edit-div" class='data-popup ignore'>
            <div class="bd">
                <form novalidate action="{% url "cloud-microsoft-365-edit" %}" method="post" id="edit-form">
                    <table class="form-data">
                        <tr>
                            <td style="vertical-align:middle;width:13%">
                                <label for="id_name">
                                Name
                                </label>
                            </td>
                            <td>{{ edit_form.name }}</td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <label>
                                Active
                                {{ edit_form.is_active }}
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="id_tenant_id">
                                Tenant ID
                                </label>
                            </td>
                            <td>
                                {{ edit_form.tenant_id }}
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="id_client_id">
                                Client ID
                                </label>
                            </td>
                            <td>
                                {{ edit_form.client_id }}
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="id_secret_value">
                                Secret value
                                </label>
                            </td>
                            <td>
                                {{ edit_form.secret_value }}
                            </td>
                        </tr>
                        <tr>
                            <td style="vertical-align:middle">
                            OAuth2
                            </td>
                            <td>
                            <a href="javascript:$.fn.zato.toggle_visibility('.oauth2-options-block')">Toggle options</a>
                            </td>
                        </tr>
                        <tr class="oauth2-options-block hidden">
                            <td>
                                <label for="id_scopes">
                                Scopes
                                </label>
                            </td>
                            <td>
                                {{ edit_form.scopes }}
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align:right">
                                <input type="submit" value="OK" />
                                <button type='button' onclick='javascript:$.fn.zato.data_table.close(this)'>Cancel</button>
                            </td>
                        </tr>
                    </table>
                    <input type="hidden" id="id_edit-cluster_id" name="cluster_id" value="{{ cluster_id }}" />
                    <input type="hidden" id="id_edit-id" name="id" />
                </form>
            </div>
        </div>

        <div id="change_password-div" class='data-popup ignore'>
            <div class="bd">
                <form novalidate action="{% url "cloud-microsoft-365-reset-oauth2-scopes" %}" method="post" id="change_password-form">
                    <table class="form-data">
                        <tr style="padding-bottom: 0.5rem;">
                            <td style="width:12%">Name</td>
                            <td id="change-password-name"></td>
                        </tr>
                        <tr>
                            <td style="vertical-align:top">
                                Step #1
                            </td>
                            <td style="vertical-align:top !important; padding-bottom: 0.5rem">
                                In a new tab, log in to Azure and open the address below
                                <textarea id="reset_oauth2_scopes_url_step_1" style="margin-top:0.3rem; margin-bottom:0.3rem; height:7rem"></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td style="vertical-align:top">
                                Step #2
                            </td>
                            <td style="line-height: 1rem">
                                Paste below the address of the page that Azure redirected you to
                                <br/>
                                <textarea id="reset_oauth2_scopes_url_step_2" name="reset_oauth2_scopes_url_step_2" style="margin-top:0.3rem; margin-bottom:0.3rem; height:4rem"></textarea>
                                <input type="hidden" id="id_change_password-id" name="id" />
                                <input type="hidden" id="id_change_password-cluster_id" name="cluster_id" value='{{ cluster_id}}' />
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align:right">
                                <input type="submit" value="OK" />
                                <button type='button' onclick='javascript:$.fn.zato.data_table.close(this)'>Cancel</button>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>

        {% endif %}


{% endif %}{% comment %}not zato_clusters{% endcomment %}

{% endblock %}
